package de.gameduell.framework.gui.style {
	
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.util.HashMap;
	import de.gameduell.framework.util.Library;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	
	
	/**
	 * style descriptor for each entry in style.xml
	 **/
	public class StyleDescriptor {
		
		// key="up" value="libraryClass"
		protected var stateViewClassMap:HashMap;
		
		// key="up" value="true" (generate text field?)
		protected var stateTextFlagMap:HashMap;

		// key="up" value="Point(0,2)"
		protected var stateTextOffsetMap:HashMap;

		// key="up" value="Point(100,200)"
		protected var stateTextSizeMap:HashMap;
				
		public function StyleDescriptor() {
			stateViewClassMap = new HashMap();
			stateTextFlagMap = new HashMap();
			stateTextOffsetMap = new HashMap();
			stateTextSizeMap = new HashMap();
		}	
				
		// inject values from config
		public function registerState(stateType:String, libraryId:String, generateText:Boolean, offset:Point, size:Point):void {
			stateViewClassMap.put(stateType, libraryId);
			stateTextFlagMap.put(stateType, generateText);
			stateTextOffsetMap.put(stateType, offset);
			stateTextSizeMap.put(stateType, size);
			
			Debug.trace("stateType="+stateType+ " libraryId="+libraryId+" generateText="+generateText + " offset=" + offset + " size=" + size);
		}
		
		// add states and text's to sprite
		// -> allow to use before button creation or Popup.registerButton()
		public function build(container:Sprite):void {
			
			// clean dummy states
			while(container.numChildren > 0) {
				container.removeChildAt(0);
			}
			
			// add registered states
			for each(var stateType:String in stateViewClassMap.keys) {
				var className:String = stateViewClassMap.getValue(stateType) as String;
				var generateText:Boolean = stateTextFlagMap.getValue(stateType) as Boolean;
				
				// create state container
				var stateContainer:Sprite = new Sprite();
				stateContainer.name = stateType;
				container.addChild(stateContainer);
				
				Debug.trace("StyleDescriptor.build() stateType=" + stateType + " className="+className+" generateText=" + generateText);
				
				// create sprite for className
				var stateSprite:Sprite = Library.getInstance().createSprite(className);
				stateSprite.name = "bg";
				stateContainer.addChild(stateSprite);
				
				// generate text, apply simple transforms for right position
				if (generateText) {
					
					// use existing text or create it?
					var textField:TextField = stateContainer.getChildByName("label") as TextField;
					if (textField == null) {
						textField = new TextField();
					}					
					
					
					var offset:Point = stateTextOffsetMap.getValue(stateType) as Point;
					var size:Point = stateTextSizeMap.getValue(stateType) as Point;
					textField.name = "label";
					textField.x = offset.x;
					textField.y = offset.y;
					
					// set size if not (0,0)
					if (size.x != 0 && size.y != 0) {
						textField.width = size.x;
						textField.height = size.y;
					}
					stateContainer.addChild(textField);
				}
				
			}
		}

	}
}